package com.jingdianjichi.oss.service;

import com.jingdianjichi.oss.adapter.StorageAdapter;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件存储service
 */
@Service
public class FileService {

    private final StorageAdapter storageAdapter;


    public FileService(StorageAdapter storageAdapter){
        this.storageAdapter = storageAdapter;
    }


    /**
     * 列出所以桶
     */
    public List<String> getAllBucket(){
        return storageAdapter.getAllBucket();
    }

    /**
     * 获取文件路径
     * @param buckName
     * @param objectName
     * @return
     */
    public String getUrl(String buckName, String objectName){
      return  storageAdapter.getUrl(buckName,objectName);
    }


    /**
     * 上传文件
     */
    public String uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
       storageAdapter.uploadFile(uploadFile,bucket,objectName);
       objectName = objectName + "/" + uploadFile.getOriginalFilename();
        return storageAdapter.getUrl(bucket, objectName);

    }
}


